package com.interview.employeeInfo.repository;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.interview.core.page.Page;
import com.interview.employeeInfo.entity.EmployeeInfo;
import com.interview.employeeInfo.entity.EmployeeInfo_;
import com.interview.employeeInfo.query.EmployeeInfoConditionQuery;

@Repository
public class EmployeeInfoRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public Page dynamicQuery(
			EmployeeInfoConditionQuery employeeInfoConditionQuery) {

		String name = employeeInfoConditionQuery.getName();
		String grender = employeeInfoConditionQuery.getGrender();
		Integer pageIndex = employeeInfoConditionQuery.getPageIndex();

		List<Predicate> queryCon = new ArrayList<Predicate>();

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<EmployeeInfo> query = criteriaBuilder
				.createQuery(EmployeeInfo.class);

		Root<EmployeeInfo> root = query.from(EmployeeInfo.class);

		if (name != null) {

			Predicate predicateName = criteriaBuilder.like(
					root.get(EmployeeInfo_.name), "%" + name + "%");
			queryCon.add(predicateName);
		}
		
		if (!grender.equals("All")) {
			Predicate predicateGrender = criteriaBuilder.equal(
					root.get(EmployeeInfo_.grender), grender);
			queryCon.add(predicateGrender);
		}

		query.where(criteriaBuilder.and(queryCon.toArray(new Predicate[queryCon
				.size()])));

		TypedQuery<EmployeeInfo> type = entityManager.createQuery(query);

		Integer total = type.getResultList().size();

		if (pageIndex != 0) {

			type.setFirstResult(pageIndex * 10).setMaxResults(10);

		} else {

			type.setFirstResult(0).setMaxResults(10);

		}

		List<EmployeeInfo> employeeInfoes = type.getResultList();
		Page page = new Page();
		page.setList(employeeInfoes);
		page.setPageIndex(pageIndex);
		page.setTotal(total);

		return page;
	}

	public EmployeeInfo save(EmployeeInfo employeeInfo) {
		entityManager.persist(employeeInfo);
		return employeeInfo;

	}

	public void delet(String id) {

		entityManager.remove(findOne(id));

	}

	public EmployeeInfo update(EmployeeInfo employeeInfo) {
		return entityManager.merge(employeeInfo);

	}

	public EmployeeInfo findOne(String id) {
		return entityManager.find(EmployeeInfo.class, id);
	}
	
	public List<EmployeeInfo>  findAll(){
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<EmployeeInfo> query = criteriaBuilder
				.createQuery(EmployeeInfo.class);

		Root<EmployeeInfo> root = query.from(EmployeeInfo.class);

		TypedQuery<EmployeeInfo> type = entityManager.createQuery(query);

		List<EmployeeInfo> employeeInfoes = type.getResultList();
		
		return employeeInfoes;
		
	}
}
